<template>
  <div class="login-container">
    <div class="login-content">
      <!-- 左侧工作区图片 -->
      <div class="workspace-image">
        <img src="/login_back.png" alt="workspace" />
      </div>
      
      <!-- 右侧登录表单 -->
      <div class="login-form-container">
        <div class="login-form">
          <h2>账号登录</h2>
          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            class="form-content"
           >
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                placeholder="请输入用户名"
              >
                <template #prefix>
                  <el-icon><User /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                placeholder="请输入密码"
                type="password"
                show-password
              >
                <template #prefix>
                  <el-icon><Lock /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <div class="agreement-row">
              <el-checkbox v-model="isAgree">
                用户平台使用协议
              </el-checkbox>
            </div>

            <el-button
              :loading="loading"
              type="primary"
              class="login-button"
              :disabled="!isAgree"
              @click="handleLogin"
            >
              登录
            </el-button>

            <div class="other-login">
              <span class="account-login">账号登录</span>
              <span class="divider">|</span>
              <span class="scan-login">扫码登录</span>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useUserStore } from '@/stores/user'
import type { FormInstance } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const userStore = useUserStore()
const loginFormRef = ref<FormInstance>()
const loading = ref(false)
const isAgree = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

// 用户名验证规则
const validateUsername = (rule: any, value: string, callback: any) => {
  if (!value) {
    callback(new Error('请输入用户名'))
  } else {
    callback()
  }
}

const loginRules = {
  username: [{ validator: validateUsername, trigger: 'blur' }],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码不能少于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const res= await userStore.loginAction({
          username: loginForm.username,
          password: loginForm.password
        })

        
          sessionStorage.setItem('is_new',res.data.is_new)
          sessionStorage.setItem('username',res.data.username)

   
        
        ElMessage.success('登录成功')

      } catch (error: any) {
        ElMessage.error(error.message || '登录失败')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f5f5;
  overflow: hidden;
}

.login-content {
  display: flex;
  width: 100%;
  height: 100vh;
  background: #fff;
  overflow: hidden;
}

.workspace-image {
  flex: 1.5;
  display: none;
  background: #f0f2f5;
  
  @media (min-width: 992px) {
    display: block;
  }
  
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}

.login-form-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  padding: 40px;
  
  @media (max-width: 576px) {
    padding: 20px;
  }
}

.login-form {
  width: 100%;
  max-width: 400px;
}

.login-form h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 24px;
  font-weight: 500;
}

.form-content {
  width: 100%;
}

.agreement-row {
  margin: 20px 0;
  color: #666;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  margin-bottom: 20px;
  border-radius: 22px;
}

.other-login {
  text-align: center;
  color: #666;
  font-size: 14px;
  margin-top: 20px;
}

.account-login,
.scan-login {
  cursor: pointer;
  color: #409EFF;
  
  &:hover {
    color: #66b1ff;
  }
}

.divider {
  margin: 0 10px;
  color: #dcdfe6;
}

:deep(.el-input__wrapper) {
  padding-left: 0;
}

:deep(.el-input__prefix) {
  margin-left: 10px;
}

:deep(.el-checkbox__label) {
  color: #666;
}

// 响应式调整
@media (max-width: 768px) {
  .login-content {
    height: 100vh;
  }
  
  .login-form-container {
    padding: 20px;
  }
  
  .login-form h2 {
    font-size: 20px;
    margin-bottom: 20px;
  }
}
</style> 